﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_Petition : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlPetitionSeq;
            return ddl;
        }

        private void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;

            IList<M_FIM_Petitions> infolist = (lblApproved.Text == "1" ?
                pet.GetApprovedList(strBgmSourceSeq, strBgmYear, strFilter)
                : pet.GetList(strBgmSourceSeq, strBgmYear, strFilter));
            ;
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            //ddlContactSeq.Items.Clear();

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlPetitionSeq.Items.Add(new RadComboBoxItem(infolist[i].petition_type_name, infolist[i].petition_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(string strBgmSourceSeq, string strBgmYear
            , bool bApproved, Nullable<int> nWidth, Nullable<int> nSetOffset)
        {
            if (nWidth != null)
                ddlPetitionSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblBgmSourceSeq.Text = strBgmSourceSeq;
            lblBgmYear.Text = strBgmYear;
            lblApproved.Text = (bApproved ? "1" : "0");
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue == null)
            {
                strValue = "";
            }

            if (strValue.Length > 0)
            {
                lblPetitionSeq.Text = strValue;

                if (ddlPetitionSeq.FindItemByValue(strValue) == null)
                {
                    M_FIM_Petitions info = pet.GetInfo(strValue);
                    ddlPetitionSeq.SelectedValue = strValue;
                    ddlPetitionSeq.Text = info.petition_type_name;
                }
                else
                {
                    ddlPetitionSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlPetitionSeq.SelectedIndex = -1;
                ddlPetitionSeq.Text = "";
            }

            ddlPetitionSeq_SelectedIndexChanged(null, null);
        }

        public string GetSelectedValue()
        {
            string strValue = lblPetitionSeq.Text;

            return strValue;
        }

        protected void ddlPetitionSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblPetitionSeq.Text = ddlPetitionSeq.SelectedValue;

            if (Child != null)
            {

            }

            if (Page.ToString() == "ASP.app_ui_forms_fim_receives_aspx")
            {
                Label lblPetitionAmount = (Label)Parent.FindControl("lblPetitionAmount");
                Label lblTax = (Label)Parent.FindControl("lblTax");
                Label lblPenalty = (Label)Parent.FindControl("lblPenalty");
                Label lblRedemption = (Label)Parent.FindControl("lblRedemption");

                if (ddlPetitionSeq.SelectedValue.Length > 0)
                {
                    M_FIM_Petitions info = pet.GetInfo(ddlPetitionSeq.SelectedValue);
                    lblPetitionAmount.Text = info.sum_amount_real.ToString();
                    lblTax.Text = info.sum_amount_tax.ToString();
                    lblPenalty.Text = info.sum_amount_penalty.ToString();
                    lblRedemption.Text = info.sum_amount_redemption.ToString();
                }
                else
                {
                    lblPetitionAmount.Text = "0";
                    lblTax.Text = "0";
                    lblPenalty.Text = "0";
                    lblRedemption.Text = "0";
                }
            }
        }

        protected void ddlPetitionSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}